{include file="common/pc/header" title="编辑部门" /}

<style>
    body {
        padding: 10px;
    }
</style>

<form class="layui-form" action="">
    <input type="hidden" name="id" value="{$dept.id}"/>
    <div class="layui-form-item">
        <label class="layui-form-label">部门名称</label>
        <div class="layui-input-block">
            <input type="text" name="title" required lay-verify="required"
                   placeholder="请输入部门名称" autocomplete="off" class="layui-input" value="{$dept.title}"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">上级部门</label>
        <div class="layui-input-block">
            <select name="parentId" id="parentId" lay-search></select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">部门主管</label>
        <div class="layui-input-block">
            {d:cdu name="leader" value="$leaderCdu" dept="false" user="true"}请选择主管人员{/d:cdu}
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="submit">更新</button>
        </div>
    </div>
</form>


{php}
$enterpriseJson = json_encode($enterprise);
{/php}
<script>
    var deptTree = {$deptTree};
    var deptPath = {};
    var enterprise = {$enterpriseJson};
</script>

<script>
    /**
     * 初始化部门路径
     *
     * @param currentDeptId
     * @param currentDeptPath
     * @param currentDeptTreeChildItems
     */
    function initDeptPath(currentDeptId, currentDeptPath, currentDeptTreeChildItems) {
        if (currentDeptId == '{$dept.id}') {
            return;
        }
        deptPath[currentDeptId] = currentDeptPath;
        if (!currentDeptTreeChildItems) {
            return;
        }
        $.each(currentDeptTreeChildItems, function () {
            initDeptPath(this.id, currentDeptPath.concat(this.title), this.childs);
        });
    }

    layui.use('form', function () {
        var form = layui.form;

        //初始化部门路径
        initDeptPath("1", [enterprise.title], deptTree);

        $.each(deptPath, function (index, value) {
            var selected = index == '{$dept.parent_id}' ? 'selected' : '';
            $("#parentId").append('<option value="' + index + '" ' + selected + '>' + value.join(" - ") + '</option>');
        });

        form.render();

        //监听提交
        form.on('submit(submit)', function (data) {
            g.loading();
            $.post("", data.field, function (ret) {
                g.loading(false);
                if (ret.code == 1) {
                    layer.msg(ret.msg);
                } else {
                    layer.alert(ret.msg);
                }
            }, "json");
            return false;
        });
    });

</script>

{include file="common/pc/footer"/}